//
//  HXChangeTQJRXMViewController.m
//  KD
//
//  Created by GaoFei on 16/6/28.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXChangeTQJRXMViewController.h"

#import "HXTQJRXMModel.h"
#import "HXSelectedAreaMapViewController.h"

@interface HXChangeTQJRXMViewController ()<HXSelectedAreaMapViewControllerDelegate>

@property (nonatomic,copy) NSString * keyGuid;//项目的唯一标识

/**提前介入项目*/
@property (nonatomic,retain) HXTQJRXMListModel * listModel;

/**项目时间*/
@property (weak, nonatomic) IBOutlet UITextField *xmCreateTimeTF;

/**项目名称*/
@property (weak, nonatomic) IBOutlet UITextView *xmPrjNameTV;

/**项目地址*/
@property (weak, nonatomic) IBOutlet UITextView *xmPrjAddressTV;

/**建设单位*/
@property (weak, nonatomic) IBOutlet UITextView *jsBuildCorpNameTV;

/**建设负责人*/
@property (weak, nonatomic) IBOutlet UITextField *jsBuilderTF;

/**联系电话*/
@property (weak, nonatomic) IBOutlet UITextField *jsBuilderTelTF;


@property (nonatomic,copy) NSString * PointX;
@property (nonatomic,copy) NSString * PointY;

@end

@implementation HXChangeTQJRXMViewController

//创建视图控制器
+(instancetype)initWithKeyGuid:(NSString *)keyGuid{

    HXChangeTQJRXMViewController * vc = [[HXChangeTQJRXMViewController alloc] init];
    
    vc.keyGuid = keyGuid;
    
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置页面布局
    [self loadViewControllerLayout];
    
    //获取数据
    [self getLoadData];
}

/**修改坐标*/
- (IBAction)changeCoordinateBtnAct:(id)sender {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HXInform" bundle:nil];
    HXSelectedAreaMapViewController *vc = [sb instantiateViewControllerWithIdentifier:@"HXSelectedAreaMapViewController"];
    vc.mapEnumType = MapTypeChangeCoordinateType;
    vc.delegate = self;
    vc.isSelectd = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -HXSelectedAreaMapViewControllerDelegatre
-(void)location:(NSMutableDictionary *)locationDict{

    NSLog(@"------locationDict is %@",locationDict);
    
    self.xmPrjAddressTV.text = [locationDict objectForKey:@"address"];
    
    self.PointX = [locationDict objectForKey:@"X"];
    self.PointY = [locationDict objectForKey:@"Y"];
}


/**结束提前介入*/
- (IBAction)endTQJRBtnAct:(id)sender {

    [self loadCanleOrDelete:@"3"];
}

/**删除或者取消提前介入 3:结束提前介入 4:删除*/
-(void)loadCanleOrDelete:(NSString *)kindType{

    [NetworkAPI cancelOrDeleteIntProjectWithKeyGuid:self.keyGuid withKindType:kindType Success:^(HXBaseModel *result) {
        
        if ([result.code isEqualToString:@"200"]) {
            
            [SVProgressHUD showSuccessWithStatus:@"操作成功"];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:@"操作失败,请稍后重试!"];
        }
        
    } failure:^(NSError *err) {
        
        [SVProgressHUD showErrorWithStatus:@"网络出错!"];
    }];
    
}

/**删除*/
- (IBAction)deleteBtnAct:(id)sender {
    
    [self loadCanleOrDelete:@"4"];
}

/**保存并退出*/
- (IBAction)saveAndPopBtnAct:(id)sender {

    //上传数据
    [self loadShangChuanShuJu];
}

/**上传数据*/
-(void)loadShangChuanShuJu{
    
    // ---- 安全判断
    //时间
    NSString * areaStr = [self.xmCreateTimeTF text];
    //项目名称
    NSString * nameStr = [self.xmPrjNameTV text];
    //项目地址
    NSString * addressStr = [self.xmPrjAddressTV text];
    //建设单位
    NSString * unitsStr = [self.jsBuildCorpNameTV text];
    //建设单位负责人
    NSString * principalStr = [self.jsBuilderTF text];
    //建设单位电话
    NSString * telePhoneStr = [self.jsBuilderTelTF text];
    
    //判断
    if ([GlobalUtil isNilValue:areaStr]) {
        
        [self.xmCreateTimeTF becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:nameStr]) {
        
        [self.xmPrjNameTV becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:addressStr]) {
        
        [self.xmPrjAddressTV becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:unitsStr]) {
        
        [self.jsBuildCorpNameTV becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:principalStr]) {
        
        [self.jsBuilderTF becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:telePhoneStr]) {
        
        [self.jsBuilderTelTF becomeFirstResponder];
        
        return;
    }
    
    if (self.PointY == nil || self.PointX == nil) {
        
        self.PointX = self.listModel.PointX;
        self.PointY = self.listModel.PointY;
    }
    
    //添加提前介入项目
    [NetworkAPI addIntProjectWithPrjName:nameStr
                              PrjAddress:addressStr
                           BuildCorpName:unitsStr
                                 Builder:principalStr
                              BuilderTel:telePhoneStr
                                  PointX:self.PointX
                                  PointY:self.PointY
                                KindType:@"2"
                                AreaCode:self.listModel.AreaCodeAbs
                                 KeyGuid:self.keyGuid
                                 Success:^(HXBaseModel *result) {
                                  
                                  if ([result.code integerValue] == 200) {
                                      
                                      [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
                                      
                                      [self.navigationController popViewControllerAnimated:YES];
                                      
                                  }else{
                                      
                                      [SVProgressHUD showErrorWithStatus:@"提交任务失败,请稍后再试!"];
                                  }
                                  
                              } failure:^(NSError *err) {
                                  
                                  [SVProgressHUD showErrorWithStatus:@"提交任务失败!"];
                                  
                              }];
}

/**设置页面的布局*/
-(void)loadViewControllerLayout{

    NSArray * tvtfArr = @[_xmPrjNameTV,
                          _xmPrjAddressTV,
                          _jsBuildCorpNameTV,
                          _xmCreateTimeTF,
                          _jsBuilderTF,
                          _jsBuilderTelTF];
    
    for (int i = 0 ; i < tvtfArr.count; i++) {
        
        if (i == 0 || i == 1 || i == 2) {
            UITextView * textView = tvtfArr[i];
            
            textView.layer.cornerRadius = 4.f;
            textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
            textView.layer.borderWidth = 0.5f;
            
        }else{
        
            UITextField * textField = tvtfArr[i];
            
            textField.layer.cornerRadius = 4.f;
            textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            textField.layer.borderWidth = 0.5f;

        }
    }
}

/**获取数据*/
-(void)getLoadData{

    [SVProgressHUD showWithStatus:@"正在加载..."];
    
    [NetworkAPI getYellowProgectStateWithKeyGuid:self.keyGuid Success:^(HXTQJRXMModel *result) {
        
        [SVProgressHUD dismiss];
        
        if ([result.code isEqualToString:@"200"]) {
            
            self.listModel = result.Projectinfo;
            
            self.navigationItem.title = self.listModel.PrjName;
            
            self.xmCreateTimeTF.text = self.listModel.CreateTime;
            self.xmPrjNameTV.text = self.listModel.PrjName;
            self.xmPrjAddressTV.text = self.listModel.PrjAddress;
            self.jsBuilderTF.text = self.listModel.Builder;
            self.jsBuilderTelTF.text = self.listModel.BuilderTel;
            self.jsBuildCorpNameTV.text = self.listModel.BuildCorpName;
            
        }else{
        
            [SVProgressHUD showErrorWithStatus:@"获取数据出错"];
        }
        
    } failure:^(NSError *err) {
       
        [SVProgressHUD showErrorWithStatus:@"网络错误!"];

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
